package routes

import (
	"fmt"
	v1 "ginblog/api/v1"
	"ginblog/middleware"
	"ginblog/utils"
	"github.com/gin-gonic/gin"
	"time"
)

//初始化操作
func init() {
	gin.SetMode(utils.AppMode)

	// 禁用控制台颜色
	//gin.DisableConsoleColor()

	// 创建记录日志的文件
	//f, _ := os.Create("runtime/api/gin.log")
	//gin.DefaultWriter = io.MultiWriter(f)
}

func InitRouter() {
	//自定义日志格式 使用logrus
	router := gin.New()

	router.Use(middleware.Logger())
	router.Use(gin.Recovery())
	router.Use(middleware.Cors())

	auth := router.Group("api/v1")
	auth.Use(middleware.JwtToken())
	{
		//用户模块的路由接口
		UserRoutes(auth)

		//分类模块的路由接口
		CategoryRoutes(auth)

		//文章模块的路由接口
		ArticleRoutes(auth)


	}

	publicRouter := router.Group("api/v1")
	{
		publicRouter.POST("/login", v1.Login)

		publicRouter.POST("/upload", v1.UploadFile)
	}

	router.Run(utils.HttpPort)
}


func InitRouter_gin() {
	//router := gin.Default()

	//自定义日志格式 gin框架自带
	router := gin.New()

	// LoggerWithFormatter 中间件会将日志写入 gin.DefaultWriter
	// By default gin.DefaultWriter = os.Stdout
	router.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {

		// 你的自定义格式
		return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" %s\"\n",
			param.ClientIP,
			param.TimeStamp.Format(time.RFC3339),
			param.Method,
			param.Path,
			param.Request.Proto,
			param.StatusCode,
			param.Latency,
			param.Request.UserAgent(),
			param.ErrorMessage,
		)
	}))
	router.Use(gin.Recovery())

	auth := router.Group("api/v1")
	auth.Use(middleware.JwtToken())
	{
		//用户模块的路由接口
		UserRoutes(auth)

		//分类模块的路由接口
		CategoryRoutes(auth)

		//文章模块的路由接口
		ArticleRoutes(auth)


	}

	publicRouter := router.Group("api/v1")
	{
		publicRouter.POST("/login", v1.Login)

		publicRouter.POST("/upload", v1.UploadFile)
	}

	router.Run(utils.HttpPort)
}
